import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;

/**
 * Multi-threaded proxy server listens for connections and launches a new thread for each
 * connection
 * @author hussein
 */
public class MTServer {

    private ServerSocket server;
    private boolean running;
    private HashMapCache<URL, byte[]> cache;
    
    public MTServer(int port, boolean hasCache) throws IOException {
        server = new ServerSocket(port);
        cache = new HashMapCache<URL, byte[]>(100, hasCache);
        System.out.println(hasCache);
    }

    public void startServer() {
        running = true;
        while(running) 
        {
            acceptConnection();
        }
    }

    private void acceptConnection()
    {
        try
        {
            Socket client = server.accept();
            WorkerThread wt = new WorkerThread(client, cache);
            wt.start();          
        }
        catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
